有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java My数据库已经更新,但我的安卓 studio仍然没有更新,仍然显示以前获取的数据库

如何更新到新数据库,因为我尝试更新我的数据库,但它仍然没有改变,它仍然得到旧数据库。我使用php文件连接到数据库mysql,并使用JSON解析数据

这是代码

public class TampilUser extends AppCompatActivity {

    ListView listView;
    CustomAdapter customAdapter;
    ArrayList<User> user = new ArrayList<>();

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_tampil_user);

        listView = (ListView) findViewById(R.id.listUser);

        getJSON("Link here");
    }

    @Override
    public void onResume() {
        super.onResume();

        getJSON("Link here");
    }

    private void getJSON(final String urlWebService) {

        class GetJSON extends AsyncTask<Void, Void, String> {

            @Override
            protected void onPreExecute() {
                super.onPreExecute();
            }

            @Override
            protected void onPostExecute(String s) {
                super.onPostExecute(s);
                Toast.makeText(getApplicationContext(), s, Toast.LENGTH_SHORT).show();
                try {
                    loadIntoListView(s);
                } catch (JSONException e) {
                    e.printStackTrace();
                }
            }

            @Override
            protected String doInBackground(Void... voids) {
                try {
                    URL url = new URL(urlWebService);
                    HttpURLConnection con = (HttpURLConnection) url.openConnection();
                    StringBuilder sb = new StringBuilder();
                    BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(con.getInputStream()));
                    String json;
                    sb.setLength(0);
                    while ((json = bufferedReader.readLine()) != null) {
                        sb.append(json + "\n");
                    }
                    bufferedReader.close();
                    con.disconnect();
                    return sb.toString().trim();
                } catch (Exception e) {
                    return null;
                }
            }
        }
        GetJSON getJSON = new GetJSON();
        getJSON.execute();
    }

    public void loadIntoListView(String json) throws JSONException {
        JSONArray jsonArray = new JSONArray(json);
        user = new ArrayList<>();
        user.clear();
        User u;
        for (int i = 0; i < jsonArray.length(); i++) {
            JSONObject obj = jsonArray.getJSONObject(i);
            String email = obj.getString("email");
            String nama = obj.getString("nama");

            u = new User();
            u.setEmail(email);
            u.setNama(nama);

            user.add(u);
        }
        customAdapter = new CustomAdapter(getApplicationContext(),user);
        listView.setAdapter(customAdapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                String nama = customAdapter.user.get(position).getNama();
                String email = customAdapter.user.get(position).getEmail();
                Intent i = new Intent(getApplicationContext(), DetailUser.class);

                i.putExtra("email", email);
                i.putExtra("nama", nama);
                startActivityForResult(i, 0);
            }
        });
    }
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data){
        getJSON("Link here"); 
        customAdapter.notifyDataSetChanged();
        customAdapter.notifyDataSetInvalidated();
    }
}

我使用的是自定义适配器,这是自定义适配器扩展基本适配器中的获取视图代码

public View getView(int position, View convertView, ViewGroup parent) {

   if(convertView==null)
   {
       convertView=inflater.inflate(R.layout.daftar_user,parent,false);
   }
   TextView textView_email = (TextView) convertView.findViewById(R.id.textView_email);
   TextView textView_nama = (TextView) convertView.findViewById(R.id.textView_nama);

   textView_email.setText(user.get(position).getEmail());
   textView_nama.setText(user.get(position).getNama());

   return convertView;
}

我尝试了很多东西,但仍然没有改变。。我还是stackoverflow的新手,非常感谢你的帮助。。谢谢你


共 (1) 个答案

  1. # 1 楼答案

    问题已经解决了

    我只是把自定义适配器。在单击列表视图开始新的意图之前,先通知DatasetChanged()